package id.zelory.compressor.sample;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Color;import android.os.Bundle;import android.os.Environment;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;import java.io.File;import java.io.IOException;import java.text.DecimalFormat;import java.util.Random;import id.zelory.compressor.Compressor;import io.reactivex.android.schedulers.AndroidSchedulers;import io.reactivex.functions.Consumer;import io.reactivex.schedulers.Schedulers;public class MainActivity extends AppCompatActivity { private static final int PICK_IMAGE_REQUEST = 1; private ImageView actualImageView; private ImageView compressedImageView; private TextView actualSizeTextView; private TextView compressedSizeTextView; private File actualImage; private File compressedImage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); actualImageView = (ImageView) findViewById(R.id.actual_image); compressedImageView = (ImageView) findViewById(R.id.compressed_image); actualSizeTextView = (TextView) findViewById(R.id.actual_size); compressedSizeTextView = (TextView) findViewById(R.id.compressed_size); actualImageView.setBackgroundColor(getRandomColor()); clearImage(); } public void chooseImage(View view) { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); startActivityForResult(intent, PICK_IMAGE_REQUEST); } public void compressImage(View view) { if (actualImage == null) { showError("Please choose an image!"); } else { // Compress image in main thread //compressedImage = new Compressor(this).compressToFile(actualImage); //setCompressedImage(); // Compress image to bitmap in main thread //compressedImageView.setImageBitmap(new Compressor(this).compressToBitmap(actualImage)); // Compress image using RxJava in background thread new Compressor(this) .compressToFileAsFlowable(actualImage) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<File>() { @Override public void accept(File file) { compressedImage = file; setCompressedImage(); } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) { throwable.printStackTrace(); showError(throwable.getMessage()); } }); } } public void customCompressImage(View view) { if (actualImage == null) { showError("Please choose an image!"); } else { // Compress image in main thread using custom Compressor try { compressedImage = new Compressor(this) .setMaxWidth(640) .setMaxHeight(480) .setQuality(75) .setCompressFormat(Bitmap.CompressFormat.WEBP) .setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES).getAbsolutePath()) .compressToFile(actualImage); setCompressedImage(); } catch (IOException e) { e.printStackTrace(); showError(e.getMessage()); } // Compress image using RxJava in background thread with custom Compressor /*new Compressor(this) .setMaxWidth(640) .setMaxHeight(480) .setQuality(75) .setCompressFormat(Bitmap.CompressFormat.WEBP) .setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES).getAbsolutePath()) .compressToFileAsFlowable(actualImage) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<File>() { @Override public void accept(File file) { compressedImage = file; setCompressedImage(); } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) { throwable.printStackTrace(); showError(throwable.getMessage()); } });*/ } } private void setCompressedImage() { compressedImageView.setImageBitmap(BitmapFactory.decodeFile(compressedImage.getAbsolutePath())); compressedSizeTextView.setText(String.format("Size : %s", getReadableFileSize(compressedImage.length()))); Toast.makeText(this, "Compressed image save in " compressedImage.getPath(), Toast.LENGTH_LONG).show(); Log.d("Compressor", "Compressed image save in " compressedImage.getPath()); } private void clearImage() { actualImageView.setBackgroundColor(getRandomColor()); compressedImageView.setImageDrawable(null); compressedImageView.setBackgroundColor(getRandomColor()); compressedSizeTextView.setText("Size : -"); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK) { if (data == null) { showError("Failed to open picture!"); return; } try { actualImage = FileUtil.from(this, data.getData()); actualImageView.setImageBitmap(BitmapFactory.decodeFile(actualImage.getAbsolutePath())); actualSizeTextView.setText(String.format("Size : %s", getReadableFileSize(actualImage.length()))); clearImage(); } catch (IOException e) { showError("Failed to read picture data!"); e.printStackTrace(); } } } public void showError(String errorMessage) { Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show(); } private int getRandomColor() { Random rand = new Random(); return Color.argb(100, rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); } public String getReadableFileSize(long size) { if (size <= 0) { return "0"; } final String[] units = new String[]{"B", "KB", "MB", "GB", "TB"}; int digitGroups = (int) (Math.log10(size) / Math.log10(1024)); return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) " " units[digitGroups]; }}
下载android 图片压缩示例源码用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论